
clear
set more off

cd "D:\data_replication"

// Import and format s20 and s80 data
//==============================================================================

import delimited estimation\4_demand_estimation\4_cobb_douglas_weights\share_summary_gini.csv

// Set shares to 0 if prediction is < 0
//------------------------------------------------------------------------------

forval i = 8/107 {
local j = `i' - 7	
rename share_summary`i' s_`j'
replace s_`j' = 0 if s_`j' < 0	
replace s_`j' = s_true if pc8 == 0
}


// Normalize sum of shares to 1
//------------------------------------------------------------------------------

sort year quarter declarant
by year quarter declarant: egen s_true_sum = sum(s_true)
forval i = 1/100 {
by year quarter declarant: egen s`i'_sum = sum(s_`i')
replace s_`i' = s_`i' / s`i'_sum
}



// Format and save
//------------------------------------------------------------------------------

keep pc8 year quarter declarant s_1-s_100
sort year quarter declarant pc8
order year quarter declarant pc8

save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc8_gini_temp.dta, replace



// pc8plus - disaggregated
//==============================================================================

// Get shares of synthetic categories
//------------------------------------------------------------------------------

import delimited estimation\4_demand_estimation\4_cobb_douglas_weights\Y_summary.csv, clear
gen ll = length(pc8plus)
keep if ll < 8                                                                  // Only synthetic
rename pc8plus pc8
destring pc8, replace
keep pc8 year quarter declarant share


// Combine with previous data, adjust synthetic shares to be consistent with total
//------------------------------------------------------------------------------

append using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc8_gini_temp.dta

sort year quarter declarant pc8
tostring pc8, gen(pc8_string)
gen ll = length(pc8_string)
by year quarter declarant: egen share_pc8plus = sum(share)

forval i = 1/100 {

by year quarter declarant: gen s_`i'_0_temp = s_`i' if pc8 == 0
by year quarter declarant: egen s_`i'_0 = mean(s_`i'_0_temp) 
replace s_`i' = share * s_`i'_0 / share_pc8plus if ll ==6 | ll == 7

}

drop if pc8 == 0


// Format and save
//------------------------------------------------------------------------------

keep pc8 year quarter declarant s_1-s_100
sort year quarter declarant pc8
order year quarter declarant pc8

save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc8_gini.dta, replace
export delimited using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc8_gini.csv, replace

rm estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc8_gini_temp.dta

